<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="form" label-width="220px" :model="form" :rules="rules">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="form.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>

          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <el-input v-model="form.workNumber" size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input
                  v-model="form.mobile"
                  size="mini"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <seletTree v-model="form.departmentId" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="form.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1" />
                  <el-option label="非正式" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="form.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="form.correctionTime"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <upLoadImgVue v-model="form.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="savaData">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
import seletTree from './components/selet-tree.vue'
import { addEmployeeList, getEmployeeDetail, updateEmployee } from '@/api/employee'
import upLoadImgVue from './components/upLoadImg.vue'
export default {
  components: {
    seletTree,
    upLoadImgVue
  },
  data () {
    return {
      form: {
        username: '', // 用户名
        mobile: '', // 手机号
        workNumber: '', // 工号
        formOfEmployment: null, // 聘用形式
        departmentId: null, // 部门id
        timeOfEntry: '', // 入职时间
        correctionTime: '', // 转正时间
        staffPhoto: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { mix: 1, max: 4, message: '姓名为1-4位' }
        ], // 用户名
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确',
            trigger: 'blur' }
        ], // 手机号
        // workNumber: [
        //   { required: true, message: '请输入工号', trigger: 'blur' }
        // ], // 工号
        formOfEmployment: [
          { required: true, message: '请输入聘用形式', trigger: 'blur' }
        ], // 聘用形式
        departmentId: [
          { required: true, message: '请输入部门', trigger: 'blur' }
        ], // 部门id
        timeOfEntry: [
          { required: true, message: '请输入入职时间', trigger: 'blur' }
        ], // 入职时间
        correctionTime: [
          { required: true, message: '请输入转正时间', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            if (this.form.timeOfEntry) {
              if (new Date(value) > new Date(this.form.timeOfEntry)) {
                return callback()
              } else {
                callback(new Error('转正时间不能小于入职时间'))
              }
            }
          } }
        ] // 转正时间
      }
    }
  },
  created () {
    if (this.$route.params.id) this.getDetail()
  },
  methods: {
    async savaData () {
      try {
        this.$refs.form.validate()

        if (this.$route.params.id) {
          await updateEmployee(this.form)
          this.$message.success('修改成功')
        } else {
          await addEmployeeList(this.form)

          this.$message.success('添加成功')
        }

        this.$router.push('/employee')
      } catch (error) {
        console.log(error)
      }
    },
    // 封装
    async getDetail () {
      const data = await getEmployeeDetail(this.$route.params.id)
      this.form = { ...data }
    }
  }
}
</script>

<style scoped lang="scss">
    .edit-form {
      background: #fff;
      padding: 20px;
      .inputW {
        width: 380px
      }
    }

</style>
